package cn.tjjcy.service.process;

import cn.hutool.core.util.StrUtil;
import cn.tjjcy.common.CommonUtil;
import cn.tjjcy.common.DmMsg;
import cn.tjjcy.common.FileDocUtils;
import cn.tjjcy.common.GzResult;
import cn.tjjcy.dao.TAjxxMapper;
import cn.tjjcy.domain.RealGzConfEntity;
import cn.tjjcy.domain.TAjxxEntity;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Slf4j
@Service
public class XwblProcessService extends DefaultProcessService {

    @Value("${oct.doc.path:/doc}")
    private String docPath;

    @Autowired
    private TAjxxMapper tAjxxMapper;

    //讯问时检察人员或者检察人员和书记员不少于二人
    @Override
    public GzResult gzJudge(DmMsg dmMsg, RealGzConfEntity realGzConfEntity) {
        try {
            GzResult judgeResult = super.gzJudge(dmMsg, realGzConfEntity);
            if(Objects.isNull(judgeResult)||!judgeResult.getGzResult()){
                return null;
            }
            String table = dmMsg.getTable();
            String sourceTableName = table.split("\\.")[1];
            Map<String, Object> judgeResultMap = judgeResult.getGzMap();
            String wsPath = judgeResultMap.get("ws_path").toString();
            String content = FileDocUtils.parseToString(new File(docPath + wsPath));
            String xunwenren = CommonUtil.userNameClean(content.substring(content.indexOf("讯问人") +3, content.indexOf("记录人")));
            String jiluren = CommonUtil.userNameClean(content.substring(content.indexOf("记录人") +3, content.indexOf("案由")));
            if(StrUtil.isEmpty(xunwenren)&&StrUtil.isEmpty(jiluren)){
                log.info("【规则[{}]】 表:[{}] SQL:[{}] 未提取到询问人或记录人，未触发询问笔录规则",
                        realGzConfEntity.getId(), sourceTableName, judgeResult.getGzSql());
                return GzResult.judgeFail("【规则["+realGzConfEntity.getId()+"]】 未提取到询问人或记录人，未触发询问笔录规则",judgeResult.getGzSql());
            }
            String[] itemsArray1 = xunwenren.split("、");
            String[] itemsArray2 = jiluren.split("、");
            Set<String> allNames = new HashSet<>();
            allNames.addAll(Arrays.asList(itemsArray1));
            allNames.addAll(Arrays.asList(itemsArray2));
            if(allNames.size()<2){
                log.info("【规则[{}]】 表:[{}] SQL:[{}] 触发询问笔录规则",
                        realGzConfEntity.getId(), sourceTableName, judgeResult.getGzSql());
                return GzResult.judgeSuccess(judgeResult.getGzMap(),judgeResult.getGzSql());
            }else{
                TAjxxEntity tAjxxEntity=new TAjxxEntity();
                tAjxxEntity.setXxqd("");
                tAjxxEntity.setXxzt(0);
                tAjxxEntity.setXxys("green");
                UpdateWrapper updateWrapper=new UpdateWrapper();
                updateWrapper.eq("bmsah",judgeResult.getBmsah());
                updateWrapper.eq("gzid",realGzConfEntity.getGzId());
                Integer i = tAjxxMapper.update(tAjxxEntity,updateWrapper);
                log.info("【规则[{}]】[{}] 未触发或更新审查逮捕事实及证据规则,结果:[{}]",
                        realGzConfEntity.getId(),judgeResult.getBmsah(), realGzConfEntity.getId(),i);
                return GzResult.judgeFail("【规则["+realGzConfEntity.getId()+"]】更新或未触发讯问记录规则["+i+"]"
                        ,judgeResult.getGzSql(),judgeResult.getGzMap());
            }
        }catch (Exception e){
            log.error("【规则[{}]】XwblProcessService gzJudge error",realGzConfEntity.getId(),e);
            return null;
        }
    }

}
